package com.hermit.qmst.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by hermithermit on 15/3/10.
 */
public class FileUtils {


    /**
     * 保存Image的方法，有sd卡存储到sd卡，没有就存储到手机目录
     *
     * @param fileName
     * @param bitmap
     * @throws IOException
     */
    public void savaBitmap(String fileDir, String fileName, Bitmap bitmap,String type) throws IOException {

        if (bitmap == null){
            return;
        }

        String dirPath = fileDir;
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }

        File imgFile = new File(dirPath + File.separator + fileName);
        if (imgFile.exists()){
            return;
        }else {
            imgFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(imgFile);
        if (type.equals("PNG")) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
        }else {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        }
        fos.flush();
        fos.close();

    }

    /**
     * 从手机或者sd卡获取Bitmap
     *
     * @param fileName
     * @return
     */
    public Bitmap getBitmap(String fileDir,String fileName) {
        Bitmap bitmap = null;

        try {
            bitmap  = BitmapFactory.decodeFile( fileDir + File.separator + fileName);
        }catch (OutOfMemoryError e){
            if (bitmap != null && !bitmap.isRecycled()){
                bitmap.recycle();
            }
        }



        return bitmap;
    }

    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @return
     */
    public boolean isFileExists(String fileDir,String fileName) {
        return new File(fileDir + File.separator + fileName).exists();
    }
    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @return
     */
    public static boolean isFileExists(String fileName) {
        return new File(File.separator + fileName).exists();
    }

    /**
     * 获取文件的大小
     *
     * @param fileName
     * @return
     */
    public long getFileSize(String fileDir, String fileName) {
        return new File(fileDir + File.separator + fileName).length();
    }



    /**
     * 删除SD卡或者手机的缓存图片和目录
     * 根据日期来删除图片
     */
    public void deleteFileByDate(String fileDir) {
        File dirFile;// 文件
        File childFile;// 子文件
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 系统当前时间
        String currentTime = simpleDateFormat.format(new Date());
        dirFile = new File(fileDir);

        if (dirFile.exists() && dirFile.isDirectory()) {
            String[] children = dirFile.list();
            for (int j = 0; j < children.length; j++) {
                    childFile = new File(dirFile, children[j]);
                    long lastModified = childFile.lastModified();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String date = sdf.format(new Date(lastModified));
                    int dateDay = Utils.getDateDay(date, currentTime);
                    // 如果天数大于7天，清除文件
                    if (dateDay > 7) {
                        childFile.delete();
                    }
                }
            }

    }

    /**
     * 删除SD卡或者手机的缓存图片和目录
     */
    public void deleteFile(String filePath) {
        File file = new File(filePath);

        if (file.exists() ) {
            file.delete();

        }

    }

    /**
     * 保存文件
     *
     * @param fileName 文件名
     * @param content  文件内容
     * @throws Exception
     */
    public void saveTxtFile(String fileDir,String fileName, String content) {
        FileOutputStream fos = null;
        try{

            File dir = new File(fileDir);
            if (!dir.exists()){
                dir.mkdirs();
            }

            File file = new File(fileDir, fileName);
            if (!file.exists()){
                file.createNewFile();
            }else {
                file.delete();
                file.createNewFile();
            }
            fos = new FileOutputStream(file,true);
            fos.write(content.getBytes());
            fos.flush();
        }catch (Exception e){
            e.printStackTrace();
            try{
                if (fos != null){
                    fos.close();
                }
            }catch (Exception e2){
                e2.printStackTrace();
            }
        }
    }

    public void deleteFile(String fileDir,String fileName){

       try {
           File file = new File(fileDir, fileName);
           if (file.exists()){
               file.delete();
           }
       }catch (Exception e){

       }

    }

    /**
     * 读取文件内容
     *
     * @param fileName 文件名
     * @return 文件内容
     * @throws Exception
     */
    public String readTxt(String fileDir,String fileName) throws Exception {



        FileInputStream fis = null;
        File file = null;
        file = new File(fileDir, fileName);

        if (!file.exists()){
            return null;
        }


        fis = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        byte[] data = baos.toByteArray();
        return new String(data);
    }



}
